package rc.alfresco.users;

import org.alfresco.model.ContentModel;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.security.PersonService;
import org.alfresco.service.namespace.QName;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.extensions.webscripts.AbstractWebScript;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptResponse;
import rc.alfresco.users.dao.RcUserDao;
import rc.alfresco.users.dao.RcUserDaoIbatis;
import rc.alfresco.users.dto.RcUserTEO;

import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Slejus
 * Date: 13.11.27
 * Time: 13.22
 * To change this template use File | Settings | File Templates.
 */
public class UpdateRcUser{

    private static final Logger logger = Logger.getLogger(UpdateRcUser.class);

    static final String NAMESPACE = "http://*.kada.lt/model/people/1.0";
    static final QName PROP_AMN_ID = QName.createQName(NAMESPACE, "personAmnId");

    private PersonService personService;
    private NodeService nodeService;

    private void setPersonService(PersonService personService) {
        this.personService = personService;
    }

    private void setNodeService(NodeService nodeService) {
        this.nodeService = nodeService;
    }

    public void updateUserProfile(String username) {
        logger.debug("Started: Update Rc User");
        NodeRef personNode = personService.getPerson(username);
        if (nodeService.getProperty(personNode, PROP_AMN_ID) != null) {
            String amnid = "17426579"; //nodeService.getProperty(personNode, PROP_AMN_ID).toString();
            RcUserDao manager = new RcUserDaoIbatis();
            SqlSessionFactoryInstance instance = new SqlSessionFactoryInstance();
            SqlSessionFactory sqlSessionFactory = instance.getSqlSessionFactoryInstance();
            SqlSession session = sqlSessionFactory.openSession();
            RcUserTEO gUser = manager.getUserByAmnId(Integer.parseInt(amnid), session);
            Map<QName, Serializable> properties = new HashMap<QName,Serializable>();
            properties.put(ContentModel.PROP_FIRSTNAME, gUser.getName());
            properties.put(ContentModel.PROP_LASTNAME, gUser.getSurname());
            properties.put(ContentModel.PROP_JOBTITLE, gUser.getJobTitle());
            properties.put(ContentModel.PROP_ORGANIZATION, gUser.getOrganization());
            properties.put(ContentModel.PROP_EMAIL, gUser.getEmail());
            nodeService.setProperties(personNode, properties);
            logger.debug("Ended: Update Rc User");
        }
    }

 /*
    @Override
    public void execute(WebScriptRequest webScriptRequest, WebScriptResponse webScriptResponse) throws IOException {
        RcUserDao manager = new RcUserDaoIbatis();
        SqlSessionFactoryInstance instance = new SqlSessionFactoryInstance();
        SqlSessionFactory sqlSessionFactory = instance.getSqlSessionFactoryInstance();
        SqlSession session = sqlSessionFactory.openSession();

        RcUserTEO gUser = manager.getUserByAmnId(17426579, session);

        logger.debug(gUser.getEmail());
        logger.debug(gUser.getAmnId());
        logger.debug(gUser.getId());
        logger.debug(gUser.getJobTitle());
        logger.debug(gUser.getName());
        logger.debug(gUser.getSurname());
        logger.debug(gUser.getOrganization());

        JSONObject object = new JSONObject();
        try {
            object.put("Name", gUser.getName());
            object.put("Surname", gUser.getSurname());
            object.put("Email", gUser.getEmail());
            String jsonString = object.toString();
            logger.debug(jsonString);
            webScriptResponse.setContentType("application/json; charset=utf-8");
            webScriptResponse.getWriter().write(jsonString);
        } catch (JSONException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

    }
    */
}
